package chapter07

import scala.collection.mutable

/**
  * 合并map数值
  */
object Test16_MergeMap {

  def main(args: Array[String]): Unit = {
    val map1 = Map("a" -> 1, "b" -> 3, "c" -> 6)
    val map2 = mutable.Map("a" -> 6, "b" -> 2, "c" -> 9, "d" -> 12)
    //将map1中的值合并到map2中
    val stringToInt: mutable.Map[String, Int] = map1.foldLeft(map2)((resultMap, kv) => {
      val key: String = kv._1
      val value: Int = kv._2
      resultMap(key) = resultMap.getOrElse(key, 0) + value
      resultMap
    })
    println(stringToInt)
  }
}
